using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DefenceTower : TowerHolder
{
    private float attackCounter;
    public float attackNextTime; 
    public int damage;
    public List<Transform> enemyList = new List<Transform>();

    public override void Update() {
        if(health <= 0){
            Destroy(gameObject);
        }
        AttackEnemy();
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if(other.CompareTag("Enemy")){
            if(!enemyList.Contains(other.transform)){
                enemyList.Add(other.transform);
            }
        }
    }
    private void OnTriggerExit2D(Collider2D other)
    {
        enemyList.Remove(other.transform);
    }

    public void AttackEnemy(){
        attackCounter -= Time.deltaTime;
        if(attackCounter <= 0){
            attackCounter = attackNextTime;
            if(enemyList.Count > 0){
                for (int i = 0; i < enemyList.Count; i++)
                {
                    var enemy = enemyList[i].gameObject.GetComponent<SuperEnemies>();
                    enemy.GetHurt(damage);
                    Debug.Log("群体攻击");
                }
            }
        }

    }
}
